/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
package cgl.narada.sync;

import java.net.InetAddress;

/**
 * This class represents a data structure describing  the useful
 * information that can be extracted from a SyncDatagramPacket.
 * Please see rfc2030 for more details.
 * @author Hasan Bulut  - hbulut@indiana.edu
 */
public class SyncInfo {
    //~ Instance fields ----------------------------------------------------------

    /**
     * InetAddress of the server.
     */
    private InetAddress serverAddress;
    /**
     * Offset of the local clock versus the server clock. It is calculated
     * according to NTP protocol version 3. in microsecond in this software
     */
    private long offset;
    /**
     * Originate time in milliseconds
     */
    private long originate = 0;
    /**
     * Receive time in milliseconds
     */
    private long receive = 0;
    /**
     * Reception time in milliseconds
     */
    private long reception = 0;
    /**
     * Transmit time in milliseconds
     */
    private long transmit = 0;
    /**
     * Roundtrip delay (in milliseconds). Calculated according to NTP protocol
     * version 3.
     */
    private long roundTripDelay;

    //~ Methods ------------------------------------------------------------------
    /**
     * Converts this object into a String representation.
     * @return String - String representation of this object
     */
    public String toString() {
        String s = "";
        s = "\nServer address : " + (serverAddress != null ? serverAddress.toString() : "") + "\n";
        s += "Originate time: " + originate + " ms\n";
        s += "Receive time  : " + receive + " ms\n";
        s += "Transmit time : " + transmit + " ms\n";
        s += "Reception time: " + reception + " ms\n";
        s += "Round trip delay : " + roundTripDelay + " ms\n";
        s += "Offset : " + offset + " ms";
        return s;
    }

    public long getOffset() {
        // if(Math.abs( (this.receive - this.originate) - ( this.transmit - this.reception)) < 1000 ){
        this.offset = (this.receive - this.originate -
                this.reception + this.transmit) / 2;
        return this.offset;

    }

    /**
     * @return Returns the roundTripDelay.
     */
    public long getRoundTripDelay() {
        this.roundTripDelay = this.receive - this.originate +
                this.reception - this.transmit;
        return this.roundTripDelay;
    }

    /**
     * @return Returns the originate.
     */
    public long getOriginate() {
        return originate;
    }

    /**
     * @param originate The originate to set.
     */
    public void setOriginate(long originate) {
        this.originate = originate;
    }

    /**
     * @return Returns the receive.
     */
    public long getReceive() {
        return receive;
    }

    /**
     * @param receive The receive to set.
     */
    public void setReceive(long receive) {
        this.receive = receive;
    }

    /**
     * @return Returns the reception.
     */
    public long getReception() {
        return reception;
    }

    /**
     * @param reception The reception to set.
     */
    public void setReception(long reception) {
        this.reception = reception;
    }

    /**
     * @return Returns the serverAddress.
     */
    public InetAddress getServerAddress() {
        return serverAddress;
    }

    /**
     * @param serverAddress The serverAddress to set.
     */
    public void setServerAddress(InetAddress serverAddress) {
        this.serverAddress = serverAddress;
    }

    /**
     * @return Returns the transmit.
     */
    public long getTransmit() {
        return transmit;
    }

    /**
     * @param transmit The transmit to set.
     */
    public void setTransmit(long transmit) {
        this.transmit = transmit;
    }
}
